#include "CShotgunBullet.h"
#include "..\CCyberSpaceState.h"
#include "..\CGame.h"

CShotgunBullet::CShotgunBullet(void)
{
	SetWidth(16);
	SetHeight(16);

	m_nBulletID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_SGBullet.png");

	CSGP_EventSystem::GetInstance()->RegisterClient("destroy.all.bullets", this);
}

CShotgunBullet::~CShotgunBullet(void)
{
	CSGP_EventSystem::GetInstance()->UnregisterClient("destroy.all.bullets", this);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBulletID);
}

void CShotgunBullet::Update(float fElapsedTime)
{
	CBase::Update(fElapsedTime);

	if(GetPosX() < 0 || GetPosX() > CGame::GetInstance()->GetScreenWidth()
		|| GetPosY() < 0 || GetPosY() > CGame::GetInstance()->GetScreenHeight() - GetHeight())
	{
		// Destroy the bullet
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyShotgunBulletMessage(this));
	}
	
}

void CShotgunBullet::Render(void)
{
	RECT drawRect = GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();

	CSGD_TextureManager::GetInstance()->Draw(m_nBulletID, drawRect.left, drawRect.top, 1.0f, 1.0f);

	// Draw collision box
	//CSGD_Direct3D::GetInstance()->DrawRect(GetCollisionRect(), 0, 255, 0);
}

RECT CShotgunBullet::GetCollisionRect(void)
{
	/*RECT rIntersect;
	rIntersect.left = long(GetPosX() - CCamera::GetInstance()->GetOffsetX());
	rIntersect.top = long(GetPosY() - CCamera::GetInstance()->GetOffsetY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;*/

	RECT rIntersect;
	rIntersect.left = long(GetPosX());
	rIntersect.top = long(GetPosY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;
}

bool CShotgunBullet::CheckCollision(IBaseInterface* pBase)
{

	return false;
}

void CShotgunBullet::HandleEvent(CEvent* pEvnt)
{
	if(pEvnt->GetEventID() == "destroy.all.bullets")
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyShotgunBulletMessage(this));
	}
}